﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace B2B.Models
{
    public class SanPham
    {
        int maSP;

        public int MaSP
        {
            get { return maSP; }
            set { maSP = value; }
        }
        string tenSP;

        public string TenSP
        {
            get { return tenSP; }
            set { tenSP = value; }
        }
        string hinhAnh;

        public string HinhAnh
        {
            get { return hinhAnh; }
            set { hinhAnh = value; }
        }
        Decimal gia;

        public Decimal Gia
        {
            get { return gia; }
            set { gia = value; }
        }
        int soLuongDatToiThieu;

        public int SoLuongDatToiThieu
        {
            get { return soLuongDatToiThieu; }
            set { soLuongDatToiThieu = value; }
        }
        int khaNangCungUng;

        public int KhaNangCungUng
        {
            get { return khaNangCungUng; }
            set { khaNangCungUng = value; }
        }
        string loaiThanhToan;

        public string LoaiThanhToan
        {
            get { return loaiThanhToan; }
            set { loaiThanhToan = value; }
        }
        int maDanhMuc;

        public int MaDanhMuc
        {
            get { return maDanhMuc; }
            set { maDanhMuc = value; }
        }
        string tenDanhMuc;

        public string TenDanhMuc
        {
            get { return tenDanhMuc; }
            set { tenDanhMuc = value; }
        }
        int maQuocGia;

        public int MaQuocGia
        {
            get { return maQuocGia; }
            set { maQuocGia = value; }
        }
        string tenQuocGia;

        public string TenQuocGia
        {
            get { return tenQuocGia; }
            set { tenQuocGia = value; }
        }

        int maTV;

        public int MaTV
        {
            get { return maTV; }
            set { maTV = value; }
        }

        string tenTV;

        public string TenTV
        {
            get { return tenTV; }
            set { tenTV = value; }
        }

        int maDVT;

        public int MaDVT
        {
            get { return maDVT; }
            set { maDVT = value; }
        }
        string tenDVT;

        public string TenDVT
        {
            get { return tenDVT; }
            set { tenDVT = value; }
        }
        int tinhTrang;

        public int TinhTrang
        {
            get { return tinhTrang; }
            set { tinhTrang = value; }
        }
        string chiTiet;

        public string ChiTiet
        {
            get { return chiTiet; }
            set { chiTiet = value; }
        }

       

      

        public ArrayList DanhSachSanPham(int page)
        {
            ArrayList list_SanPham = new ArrayList();
            SqlConnection conn = KetNoi.Connection();
            SqlCommand cmd = new SqlCommand("SP_DANHSACHSP", conn);
            cmd.Parameters.AddWithValue("@PAGECURENT", page);
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                SanPham sp = new SanPham();
                sp.MaSP = reader.GetInt32(0);
                sp.TenSP = reader.GetString(1);
                sp.HinhAnh = reader.GetString(2);
                sp.Gia = reader.GetDecimal(3);
                sp.SoLuongDatToiThieu = reader.GetInt32(4);
                sp.KhaNangCungUng = reader.GetInt32(5);
                sp.LoaiThanhToan = reader.GetString(6);
                sp.MaDanhMuc = reader.GetInt32(7);
                sp.TenDanhMuc = reader.GetString(8);
                sp.MaQuocGia = reader.GetInt32(9);
                sp.TenQuocGia = reader.GetString(10);
                sp.MaTV = reader.GetInt32(11);
                sp.TenTV = reader.GetString(12);
                sp.MaDVT = reader.GetInt32(13);
                sp.TenDVT = reader.GetString(14);
                sp.TinhTrang = reader.GetInt32(15);
                sp.ChiTiet = reader.GetString(16);
                list_SanPham.Add(sp);
            }
            conn.Close();
            return list_SanPham;
        }

        public SanPham LaySanPhamTheoMa(int MaSP)
        {
            SanPham sp = new SanPham();
            SqlConnection conn = KetNoi.Connection();
            SqlCommand cmd = new SqlCommand("SP_LAYSANPHAMTHEOMA", conn);
            cmd.Parameters.AddWithValue("@MASP", MaSP);
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                /*SqlConnection conn = KetNoi.Connection();
                SqlCommand cmd = new SqlCommand("SP_CAPNHATSANPHAM", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@maSP", id.MaSP);
                cmd.Parameters.AddWithValue("@tenSP", id.TenSP);
                cmd.Parameters.AddWithValue("@hinhAnh", id.HinhAnh);
                cmd.Parameters.AddWithValue("@gia", id.Gia);
                cmd.Parameters.AddWithValue("@soLuongDatToiThieu", id.SoLuongDatToiThieu);
                cmd.Parameters.AddWithValue("@khaNangCungCap", id.KhaNangCungUng);
                cmd.Parameters.AddWithValue("@maLoaiThanhToan", id.LoaiThanhToan);
                cmd.Parameters.AddWithValue("@maDanhMuc", id.MaDanhMuc);
                cmd.Parameters.AddWithValue("@maQuocGia", id.MaQuocGia);
                cmd.Parameters.AddWithValue("@maThanhVien", id.MaTV);
                cmd.Parameters.AddWithValue("@maDVT", id.MaDVT);
                cmd.Parameters.AddWithValue("@tinhTrang", id.TinhTrang);
                cmd.Parameters.AddWithValue("@chiTiet", id.ChiTiet);
                conn.Open();
                int kq = cmd.ExecuteNonQuery();
                conn.Close();
                return kq;*/
                sp.MaSP = reader.GetInt32(0);
                sp.TenSP = reader.GetString(1);
                sp.HinhAnh = reader.GetString(2);
                sp.Gia = reader.GetDecimal(3);
                sp.SoLuongDatToiThieu = reader.GetInt32(4);
                sp.KhaNangCungUng = reader.GetInt32(5);
                sp.LoaiThanhToan = reader.GetString(6);
                sp.MaDanhMuc = reader.GetInt32(7);
                sp.TenDanhMuc = reader.GetString(8);
                sp.MaQuocGia = reader.GetInt32(9);
                sp.TenQuocGia = reader.GetString(10);
                sp.MaTV = reader.GetInt32(11);
                sp.TenTV = reader.GetString(12);
                sp.MaDVT = reader.GetInt32(13);
                sp.TenDVT = reader.GetString(14);
                sp.TinhTrang = reader.GetInt32(15);
                sp.ChiTiet = reader.GetString(16);
            }
            conn.Close();
            return sp;
        }

        public int XoaSanPham(int maSP)
        {
            try
            {
                SqlConnection conn = KetNoi.Connection();
                SqlCommand cmd = new SqlCommand("SP_XOASANPHAM", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@MaSP", maSP);
                conn.Open();
                int kq = cmd.ExecuteNonQuery();
                conn.Close();
                return kq;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public int CapNhatSanPham(int MaSP, int TinhTrang)
        {
            SanPham sp = new SanPham();
            sp = LaySanPhamTheoMa(MaSP);
            sp.TinhTrang = TinhTrang;
            SqlConnection conn = KetNoi.Connection();
            SqlCommand cmd = new SqlCommand("SP_CAPNHATSANPHAM", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@maSP", sp.MaSP);
            cmd.Parameters.AddWithValue("@tenSP", sp.TenSP);
            cmd.Parameters.AddWithValue("@hinhAnh", sp.HinhAnh);
            cmd.Parameters.AddWithValue("@gia", sp.Gia);
            cmd.Parameters.AddWithValue("@soLuongDatToiThieu", sp.SoLuongDatToiThieu);
            cmd.Parameters.AddWithValue("@khaNangCungCap", sp.KhaNangCungUng);
            cmd.Parameters.AddWithValue("@maLoaiThanhToan", sp.LoaiThanhToan);
            cmd.Parameters.AddWithValue("@maDanhMuc", sp.MaDanhMuc);
            cmd.Parameters.AddWithValue("@maQuocGia", sp.MaQuocGia);
            cmd.Parameters.AddWithValue("@maThanhVien", sp.MaTV);
            cmd.Parameters.AddWithValue("@maDVT", sp.MaDVT);
            cmd.Parameters.AddWithValue("@tinhTrang", sp.TinhTrang);
            cmd.Parameters.AddWithValue("@chiTiet", sp.ChiTiet);
            conn.Open();
            int kq = cmd.ExecuteNonQuery();
            conn.Close();
            return 1;
        }

        public int TongSanPham()
        {
            SqlConnection conn = KetNoi.Connection();
            SqlCommand cmd = new SqlCommand("TONGSANPHAM", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();
            int totalRow = int.Parse(cmd.ExecuteScalar().ToString());
            conn.Close();
            return totalRow;
        }

        public int ThemSanPham(SanPham sp)
        {
            int maSPThem = -1;
            SqlConnection conn = KetNoi.Connection();
            SqlCommand cmd = new SqlCommand("SP_THEMSANPHAM", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@TENSP", sp.TenSP);
            cmd.Parameters.AddWithValue("@HINHANH", sp.HinhAnh);
            cmd.Parameters.AddWithValue("@GIA", sp.Gia);
            cmd.Parameters.AddWithValue("@SOLUONGDATTT", sp.SoLuongDatToiThieu);
            cmd.Parameters.AddWithValue("@KHANANGCUNGUNG", sp.KhaNangCungUng);
            cmd.Parameters.AddWithValue("@LOAITHANHTOAN", sp.LoaiThanhToan);
            cmd.Parameters.AddWithValue("@MADM", sp.MaDanhMuc);
            cmd.Parameters.AddWithValue("@MAQG", sp.MaQuocGia);
            cmd.Parameters.AddWithValue("@MATV", sp.MaTV);
            cmd.Parameters.AddWithValue("@MADVT", sp.MaDVT);
            cmd.Parameters.AddWithValue("@CHITIET", sp.ChiTiet);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                maSPThem = reader.GetInt32(0);
            }
            conn.Close();
            return maSPThem;
        }
    }
}